package cn.edu.baiyunu.myapplication.ui.screens.home.components

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.AccountBalance
import androidx.compose.material.icons.rounded.Add
import androidx.compose.material.icons.rounded.PieChart
import androidx.compose.material.icons.rounded.Settings
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import cn.edu.baiyunu.myapplication.ui.components.GlassmorphismCard

/**
 * 快速操作项数据类
 */
data class QuickAction(
    val icon: ImageVector,
    val name: String,
    val onClick: () -> Unit
)

/**
 * 快速操作按钮网格
 */
@Composable
fun QuickActionGrid(
    modifier: Modifier = Modifier,
    onAddTransaction: () -> Unit,
    onViewStatistics: () -> Unit,
    onBudget: () -> Unit,
    onSettings: () -> Unit
) {
    // 定义快速操作项
    val actions = listOf(
        QuickAction(
            icon = Icons.Rounded.Add,
            name = "记账",
            onClick = onAddTransaction
        ),
        QuickAction(
            icon = Icons.Rounded.PieChart,
            name = "统计",
            onClick = onViewStatistics
        ),
        QuickAction(
            icon = Icons.Rounded.AccountBalance,
            name = "预算",
            onClick = onBudget
        ),
        QuickAction(
            icon = Icons.Rounded.Settings,
            name = "设置",
            onClick = onSettings
        )
    )
    
    // 包装在GlassmorphismCard中，并为LazyVerticalGrid设置固定高度
    GlassmorphismCard(modifier = modifier) {
        LazyVerticalGrid(
            columns = GridCells.Fixed(4),
            horizontalArrangement = Arrangement.spacedBy(15.dp),
            modifier = Modifier
                .height(110.dp) // 固定高度，避免无限高度约束
                .padding(15.dp)
        ) {
            items(actions) { action ->
                QuickActionItem(action = action)
            }
        }
    }
}

/**
 * 单个快速操作按钮
 */
@Composable
private fun QuickActionItem(action: QuickAction) {
    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center,
        modifier = Modifier
            .clip(RoundedCornerShape(16.dp))
            .clickable(onClick = action.onClick)
            .padding(5.dp)
    ) {
        // 图标容器
        Box(
            modifier = Modifier
                .size(40.dp)
                .clip(CircleShape)
                .background(Color.White.copy(alpha = 0.15f)),
            contentAlignment = Alignment.Center
        ) {
            Icon(
                imageVector = action.icon,
                contentDescription = action.name,
                tint = Color.White
            )
        }
        
        // 操作名称
        Text(
            text = action.name,
            fontSize = 12.sp,
            color = Color.White,
            textAlign = TextAlign.Center,
            modifier = Modifier.padding(top = 8.dp)
        )
    }
} 